package com.itheima.consumer.controller;

import com.itheima.consumer.domain.Goods;
import com.itheima.consumer.feign.GoodsControllerFeignClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("order")
public class GoodsController {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private GoodsControllerFeignClient goodsControllerFeignClient;

    @RequestMapping("/find/{id}")
    public Goods findById(@PathVariable("id") int id) {
        /*List <ServiceInstance> instances = discoveryClient.getInstances("eureka-provider");
        ServiceInstance serviceInstance = instances.get(0);
        String host = serviceInstance.getHost();
        int port = serviceInstance.getPort();
        URI uri = serviceInstance.getUri();
        String s = uri.toString();
        System.out.println(host);
        System.out.println(port);
        System.out.println(s + "/find/1");
//        return restTemplate.getForObject("http://" + host + ":" + port + "/goods/find/1", Goods.class);
        return restTemplate.getForObject("http://eureka-provider/goods/find/1", Goods.class);*/
        return goodsControllerFeignClient.findById(id);
    }
}
